package cn.yzx.pethome.system.sdt.controller;


import cn.yzx.pethome.system.sdt.domain.SystemDictionaryType;
import cn.yzx.pethome.system.sdt.query.SystemDictionaryTypeQuery;
import cn.yzx.pethome.utils.AjaxResult;
import cn.yzx.pethome.system.sdt.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sdt")
@Api(value = "数据字典的API",description="数据字典相关的CRUD功能")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;


    @PostMapping
    @ApiOperation(value = "分页查询" )
    public AjaxResult findQuery(@RequestBody SystemDictionaryTypeQuery query){
        try {
            List<SystemDictionaryType> sdts = systemDictionaryTypeService.findByQuery(query);
            Long count = systemDictionaryTypeService.findByQueryCount(query);
            return AjaxResult.success().setData(sdts).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PutMapping
    @ApiOperation(value = "保存字典信息【有id为修改】" )
    public AjaxResult save(@RequestBody SystemDictionaryType systemDictionaryType){
        try{
            if (systemDictionaryType.getId() != null){
                systemDictionaryTypeService.update(systemDictionaryType);
            }else {
                systemDictionaryTypeService.add(systemDictionaryType);
            }
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("保存失败");
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除" )
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    @PatchMapping("/del")
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchdel(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.batchDel(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败");
    }
}